using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Experimental;
using UnityEditor.VersionControl;
using UnityEngine;

/// <summary>
/// Unity编辑器Project窗口扩展
/// </summary>
public class ProjectExtention : AssetsModifiedProcessor
{
    [MenuItem("Assets/Asset Test")]
    static void AssetTest()
    {
    }

    [InitializeOnLoadMethod]
    static void ScriptExtendBtn()
    {
        EditorApplication.projectWindowItemOnGUI += OnProjectGUI;
        EditorApplication.projectChanged += OnProjectChange;
    }

    private static void OnProjectGUI(string guid, Rect selectionrect)
    {
        if (Selection.activeObject != null)
        {
            string guidActive = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject));
            if (!string.IsNullOrEmpty(guid) && guid.Equals(guidActive))
            {
                var rect = new Rect(selectionrect.width - 100, selectionrect.y, 100, selectionrect.height);
                if (GUI.Button(rect, "Open"))
                {
                    Debug.Log("another way by double click to edit this file");
                    AssetDatabase.OpenAsset(Selection.activeObject);
                }
            }
        }
    }

    // ===========================================增删改查事件=======================================================
    
    /// <summary>
    /// 监听project窗口事件
    /// </summary>
    protected override void OnAssetsModified(string[] changedAssets, string[] addedAssets, string[] deletedAssets,
        AssetMoveInfo[] movedAssets)
    {
        if (addedAssets.Length > 0)
        {
            var assetNames = string.Join(",", addedAssets);
            Debug.Log($"{assetNames} be added to project");
        }
    }

    /// <summary>
    /// 资源发生改变调用
    /// </summary>
    private static void OnProjectChange()
    {
        
    }

    static void OnWillCreateAsset(string path)
    {
        //deprecated
    }

    static string[] OnWillSaveAssets(string[] paths)
    {
        //deprecated
        return paths;
    }

    static AssetMoveResult OnWillMoveAsset(string oldPath,string newPath)
    {
        //deprecated
        return AssetMoveResult.DidNotMove;
    }

    static AssetDeleteResult OnWillDeleteAsset(string path, RemoveAssetOptions opts)
    {
        //deprecated
        return AssetDeleteResult.DidNotDelete;
    }
}